package com.yunze.system.service.impl.newcard.card;

import com.alibaba.fastjson.JSONObject;
import com.yunze.apiCommon.upstreamAPI.YiDongEC.Inquire.Query_YD;
import com.yunze.apiCommon.utils.CardStatusReplacementUtil;
import com.yunze.common.core.domain.AjaxResult;
import com.yunze.common.mapper.mysql.newcard.card.YzNewCardMapper;
import com.yunze.common.utils.yunze.PageUtil;
import com.yunze.system.service.newcard.card.YzNewCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class YzNewCardServiceImpl implements YzNewCardService {
    @Resource
    private YzNewCardMapper cardMapper;
    @Resource
    private CardStatusReplacementUtil cardStatusReplacementUtil;


    @Override
    public AjaxResult sendToDevice(String params) {
        try {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("cd_username","C5010220A2200108034");
            hashMap.put("cd_pwd","TrP3um@ooG3I");
            hashMap.put("cd_key","0");
            //接收参数格式转换
            JSONObject object1 = JSONObject.parseObject(params);
            //对接上游api发送短信
            Query_YD queryYd = new Query_YD(hashMap, "YiDong_EC");
            String label = (String) object1.get("label");
            String iccids = (String) object1.get("iccids");
            String smsTemplate = (String) object1.get("smsTemplate");
            if (iccids.contains("\n")){
                String[] ids = iccids.split("\n");
                String result = String.join(",", ids);
                iccids = result;
            }
            String string = queryYd.sendSmsToDevice(label,iccids,smsTemplate);
            log.info(string);
            return AjaxResult.success("发送成功");
        } catch (Exception e) {
            log.error("{}", "发送到设备失败:" + e);
        }
        return AjaxResult.success("发送成功");
    }

    @Override
    public AjaxResult queryReceiveSms(String params) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(params);
            Map<String, Object> omp = new HashMap<>();
            Integer currenPage = jsonObject.get("pageNum") != null ? Integer.parseInt(jsonObject.get("pageNum").toString()) : 0;
            Integer pageSize = jsonObject.get("pageSize") != null ? Integer.parseInt(jsonObject.get("pageSize").toString()) : 10;
            Integer rowCount = cardMapper.queryReceiveSmsCount(jsonObject);
            rowCount = rowCount != null ? rowCount : 0;
            PageUtil pu = new PageUtil(rowCount, currenPage, pageSize);//初始化分页工具类
            //数据打包
            jsonObject.put("StarRow", pu.getStarRow());
            jsonObject.put("PageSize", pu.getPageSize());
            omp.put("Pu", pu);
            omp.put("Data", cardMapper.queryReceiveSms(jsonObject));
            return AjaxResult.success(omp);
        }catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }




}